# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Contributor: Sergei Lukin <sergej.lukin@gmail.com>
# Contributor: Jiri Horner <laeqten@gmail.com>
# Maintainer: knuxify <knuxify@gmail.com>
pkgname=webkit2gtk-5.0
pkgver=2.36.4
pkgrel=0
pkgdesc="Portable web rendering engine WebKit for GTK+ - GTK4 version"
url="https://webkitgtk.org/"
# blocked by gtk4.0
arch="all !s390x !riscv64"
license="LGPL-2.0-or-later AND BSD-2-Clause"
depends="bubblewrap xdg-dbus-proxy dbus:org.freedesktop.Secrets"
makedepends="
	bison
	cmake
	enchant2-dev
	flex
	geoclue-dev
	gnutls-dev
	gobject-introspection-dev
	gperf
	gst-plugins-bad-dev
	gst-plugins-base-dev
	gstreamer-dev
	gtk4.0-dev
	hyphen-dev
	icu-dev
	lcms2-dev
	libgcrypt-dev
	libjpeg-turbo-dev
	libmanette-dev
	libnotify-dev
	libpng-dev
	libseccomp-dev
	libsecret-dev
	libsoup3-dev
	libwebp-dev
	libwpe-dev
	libwpebackend-fdo-dev
	libxcomposite-dev
	libxml2-dev
	libxslt-dev
	libxt-dev
	mesa-dev
	openjpeg-dev
	openjpeg-tools
	pango-dev
	python3
	ruby
	samurai
	sqlite-dev
	woff2-dev
	"
options="!check" # upstream doesn't package them in release tarballs: Tools/Scripts/run-gtk-tests: Command not found
subpackages="$pkgname-dev $pkgname-lang $pkgname-dbg"
source="https://webkitgtk.org/releases/webkitgtk-$pkgver.tar.xz"
builddir="$srcdir/webkitgtk-$pkgver"

# secfixes:
#   2.36.4-r0:
#     - CVE-2022-22677
#     - CVE-2022-22710

build() {
	# TODO: both of the below memory issues are solved with clang, but
	# clang fails to build armv7 only due to some NEON related thing.
	case "$CARCH" in
	armhf|x86)
		# disable _FORTIFY_SOURCE to work around:
		# cc1plus: out of memory allocating 65536 bytes after a total of 3131101184 bytes
		CXXFLAGS="$CXXFLAGS -U_FORTIFY_SOURCE"
		;;
	esac

	# reduce memory usage on 32 bit (with dbg symbols)
	# https://bugs.webkit.org/show_bug.cgi?id=199272
	export CXXFLAGS="$CXXFLAGS -g1"

	cmake -B build -G Ninja \
		-DPORT=GTK \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_SKIP_RPATH=ON \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIB_INSTALL_DIR=/usr/lib \
		-DENABLE_GLES2=ON \
		-DENABLE_GTKDOC=OFF \
		-DENABLE_GEOLOCATION=ON \
		-DENABLE_JOURNALD_LOG=OFF \
		-DENABLE_SAMPLING_PROFILER=OFF \
		-DENABLE_MINIBROWSER=ON \
		-DUSE_WPE_RENDERER=ON \
		-DUSE_WOFF2=ON \
		-DUSE_GTK4=ON
	cmake --build build
}

check() {
	ninja -C build check
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	mv "$pkgdir"/usr/bin/WebKitWebDriver "$pkgdir"/usr/bin/WebKitWebDriver-5.0
}

sha512sums="
c5365c682ba4e8e6ad891638b5a82e74e38fa0867ce6c8e4e08a9dc15b862a6d3edc096a049e0711306972d6162d79fe90a0e76d8b395001798005e4488467f2  webkitgtk-2.36.4.tar.xz
"
